/**
* @author attilax 1466519819@qq.com
* @version  c0
* Copyright 2013 attilax reserved.
* All content (including but not limited to text, pictures, etc.) have copyright restrictions, the note license.
* all the software source code  copyright belongs to the original owner.
* Creative Commons protocol use please follow the "sign for non-commercial use consistent"; we do not welcome the large-scale duplication, and all rights reserved.
* Commercial sites or unauthorized media may not copy software source code.
**/
 
 


 
package com.mijie.homi.search.service.index;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
public class TaskService {

	@Autowired
	private TopicIndexService topicIndexService;
	@Autowired
	private UserIndexService userIndexService;
	@Autowired
	private MoodUserIndexService moodUserIndexService;
	
	private Timer topicIndexTaskWorker = new Timer();
	private Timer userIndexTaskWorker = new Timer();
	private Timer moodUserIndexTaskWorker = new Timer();
	
	private static final int ONE_MINUTE = 1000 * 60;
	private static final int TEN_MINUTE = ONE_MINUTE * 10;
	private static final int HALF_HOUR = TEN_MINUTE * 3 ;
	@PostConstruct
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
	private void initTask(){
		TimerTask topicIndexTask = new TimerTask(){
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
			public void run(){
				Calendar cal = Calendar.getInstance();
				//凌晨1点到5点不进行全量索引
				if(cal.get(Calendar.HOUR_OF_DAY)>=1 && cal.get(Calendar.HOUR_OF_DAY)<5)
					return ;
				topicIndexService.reIndex();
			}
		};
		topicIndexTaskWorker.schedule(topicIndexTask, ONE_MINUTE, HALF_HOUR);
		
		TimerTask userIndexTask = new TimerTask(){
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
			public void run(){
				Calendar cal = Calendar.getInstance();
				if(cal.get(Calendar.HOUR_OF_DAY)>=1 && cal.get(Calendar.HOUR_OF_DAY)<5)
					return ;
				userIndexService.reIndex();
			}
		};
		userIndexTaskWorker.schedule(userIndexTask, ONE_MINUTE , TEN_MINUTE * 2);
		
		TimerTask moodUserIndexTask = new TimerTask(){
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
			public void run(){
				Calendar cal = Calendar.getInstance();
				if(cal.get(Calendar.HOUR_OF_DAY)>=1 && cal.get(Calendar.HOUR_OF_DAY)<5)
					return ;
				moodUserIndexService.reIndex();
			}
		};
		moodUserIndexTaskWorker.schedule(moodUserIndexTask, ONE_MINUTE , TEN_MINUTE * 2);
		
	}
}